/**
 * @author helio
 */
var StorageReceiveView = Ext.extend(BasicPanel,{
	
	initlize : function() {
		
		if(!this.detailsGrid) {
						
			this.detailsStore = new Ext.data.Store({
			    proxy: new Ext.ux.data.PagingMemoryProxy(this.detailsJson),
			    //remoteSort:true,
			    reader: new Ext.data.JsonReader({
					root: 'receives',
					remoteSort: true,
			        fields: ['index', 'bcode','pcode', 'pname', {name:'pnumber',type:'float'}, 'punit', 'pamount', {name:'rnumber',type:'float'}, {name:'number',type:'float'}, 'storage_out', 'storage_in', 'type']
			    })
			});
			
			// create paging bar
			var pagingBar = new Ext.PagingToolbar({
		        pageSize: 25,
		        store: this.detailsStore,
		        displayInfo: true,
								
				plugins: new Ext.ux.ProgressBarPager()
		    });
			
			// create the grid
			var csm = new Ext.grid.CheckboxSelectionModel();
			
			this.detailsGrid = new Ext.grid.GridPanel({
				store: this.detailsStore,
				columns: [csm,{
					header: "订单编号",
					dataIndex: 'bcode',
					width: 120,
					sortable: true
				}, {
					header: "商品编号",
					dataIndex: 'pcode',
					width: 80,
					sortable: true
				}, {
					header: "商品名称",
					dataIndex: 'pname',
					sortable: true
				}, {
					header: "应入库数",
					dataIndex: 'pnumber',
					align: 'right',		
					width: 70,
					sortable: true
				}, {
					header: "单位",
					dataIndex: 'punit',
					width: 40
				}, {
					header: "已入库数",
					dataIndex: 'rnumber',
					align: 'right',
					width: 70,
					sortable: true
				}, {
					header: "入库数量",
					dataIndex: 'number',
					align: 'right',		
					width: 70,
					editor: new Ext.form.NumberField({
	               		allowBlank: true
	           		})
				}],
				
				sm: csm,
				
				tbar:[{
		            text:'接收入库',
		            tooltip:'接收入库',
		            iconCls:'accept',
					handler: this.receiveProducts.createDelegate(this)
		        }, '-', {
		            text:'新入库单',
		            tooltip:'新添加入库单',
		            iconCls:'add',
					handler: this.showInputWindow.createDelegate(this)
		        }],
				
				bbar: pagingBar,
				
				viewConfig: {
			        forceFit: true,
					enableRowBody:true,
		            showPreview:true
			    },
		
				loadMask: true,
				stripeRows: true,
				border: false,
				autoExpandColumn: 'pname',
				layout: 'fit',
				
				listeners: {
					cellclick : function(grid, rowIndex, columnIndex, e) {
						this.selectedRowIndex = rowIndex;
					}.createDelegate(this),
					
					rowdblclick : this.receiveProducts.createDelegate(this)
				}
			});
		}
	},
	
	initlizeDataHandler : function(resultJson) {
		this.detailsJson['receives'] = resultJson.receives;
		this.detailsStore.load({params:{start:0, limit:25}});
	},
	
	getCorePanel : function() {
		if (!this.detailsGrid) {
			this.initlize();
			SALE.Action.searchReceiveBillDataJson(this.initlizeDataHandler.createDelegate(this),{status: "1"});
		}
		return this.detailsGrid;
	},
	
	receiveProducts : function() {
		this.selectedRecordList = this.detailsGrid.getSelectionModel().getSelections();
		if(this.selectedRecordList.length == 0) {
			this.showMessage('请选择一条数据！！！', '警告');
		} else if(this.selectedRecordList.length > 1) {
			Ext.MessageBox.alert('警告', '每次只能入库一条数据，请确认只选中了一条数据！！！');
		} else {
			this.task = "modify";
			this.formDataJson = this.selectedRecordList[0].data;
			var checkWin = new StorageReceiveView.ReceiveCheckWindow({
				formData:this.formDataJson
			});
			checkWin.show();
			checkWin.setCallback(this.saveSuccessHandler.createDelegate(this))
		}
	},
	
	showInputWindow : function() {
		var inputWin = new StorageReceiveView.ReceiveInputWindow();
		inputWin.show();
		inputWin.setCallback(this.saveSuccessHandler.createDelegate(this));
	}
});

StorageReceiveView.showReceivePanel = function(){
	var  productsView = new StorageReceiveView();
	
	SALE.App.showOperateTabPanel(productsView.getCorePanel(), "接收入库");
}

StorageReceiveView.ReceiveCheckWindow = Ext.extend(BasicPanel,{
	
	popupWindow : null,
	issure : false,
	callback : null,
	
	initlize : function() {
		if (!this.popupWindow) {
			this.inputForm = new Ext.FormPanel({
				layout : 'form',
				margins: '10 0 5 5',
				bodyStyle : 'background-color: transparent;padding:8px 8px 2px;',
				border : false,
				labelWidth: 90,
				defaultType: 'textfield',
				
				items:  {
			        xtype:'fieldset',
					title: '请确认入库信息准确无误！',
					autoHeight:true,
					layout:'form',
					items: [{
						xtype: 'displayfield',
						fieldLabel: '商品编号',
						name: 'pcode',
						anchor: '95%'
					}, {
						xtype: 'displayfield',
						fieldLabel: '商品名称',
						name: 'pname',
						anchor: '100%'
					}, {
						xtype: 'displayfield',
						fieldLabel: '采购(调拨)数量',
						name: 'pnumber',
						anchor: '95%'
					}, new Ext.form.ComboBox({
							tpl: comboxTpl,
							fieldLabel: '接收仓库',
						    store: DataStoreUtil.getStorageStore(),
						    displayField:'name',
						    typeAhead: true,
						    mode: 'local',
						    forceSelection: false,
						    triggerAction: 'all',
						    emptyText:'默认仓库',
						    selectOnFocus:true,
							name: 'storage_in',
								
							anchor:'80%'
					}), {
						xtype: 'numberfield',
						fieldLabel: '本次接收数量',
						name: 'number',
						anchor: '60%'
					}]
				},
					
				
				buttons: [{
					text: '入库',
					listeners: {
						"click": function(){
							this.synchronizeJson();
							if(this.formDataJson.number && this.formDataJson.number != "") {								
								this.issure = true;
							}	
							this.popupWindow.hide();													
						}.createDelegate(this)
					}
				}, {
					text: '取消',
					listeners: {
						"click": function(){
							this.issure = false;
							this.popupWindow.hide();
						}.createDelegate(this)
					}
				}]
			});
			
			this.popupWindow = new Ext.Window({
		 		
		 		title: '确认入库信息',
				closable: true,	
				collapsible  : true,
				width: 400,
				height: 250,
				frame: true,
				layout: 'fit',
				closeAction :'hide',
					
				items: this.inputForm,
					
				listeners: {
					'show': function() {
						this.synchronizeForm();
					}.createDelegate(this),
						
					'hide': function() {							
						this.synchronizeJson();
						if(this.issure) {
							SALE.Action.saveReceiveBillData(this.callback, this.formDataJson);
						}						
					}.createDelegate(this)
				}
		 	});			
		}
	},
	
	show : function() {
		if (!this.popupWindow) {			
			this.initlize();
		}
		
		this.popupWindow.show();
	},
	
	setCallback : function(callback) {
		this.callback = callback;
	}
});

StorageReceiveView.ReceiveInputWindow = Ext.extend(BasicPanel,{
	
	popupWindow : null,
	issure : false,
	callback : null,
	
	initlize : function() {
		if (!this.popupWindow) {
			this.inputForm = new Ext.FormPanel({
				layout : 'form',
				frame : true,
				labelWidth: 60,
				defaultType: 'textfield',
				
				items:  {
			        xtype:'fieldset',
					title: '填写入库单信息',
					autoHeight:true,
			        anchor:'100%',
					layout:'form',
					items: [{
	                	xtype:'trigger',
	                    triggerClass: 'x-form-search-trigger',
	                    name: 'pcode',
	                    fieldLabel: '商品编号',
	                    anchor:'80%',
	                    onTriggerClick: function(){
							var pcode = this.inputForm.form.findField("pcode").getValue();
		   					var condition = {querytype:1,queryvalue:pcode};
		   					SALE.window.showProductsSelectWindow(this.productsSelectedHandler.createDelegate(this),condition);
			       		}.createDelegate(this)
	                }, {
	                	xtype:'trigger',
	                    triggerClass: 'x-form-search-trigger',
	                    name: 'pname',
	                    fieldLabel: '商品名称',
	                    anchor:'80%',
	                    onTriggerClick: function(){
							var pname = this.inputForm.form.findField("pname").getValue();
		   					var condition = {querytype:2,queryvalue:pname};
		   					SALE.window.showProductsSelectWindow(this.productsSelectedHandler.createDelegate(this),condition);
			       		}.createDelegate(this)
	                }, new Ext.form.ComboBox({
							tpl: comboxTpl,
							fieldLabel: '接收仓库',
						    store: DataStoreUtil.getStorageStore(),
						    displayField:'name',
						    typeAhead: true,
						    mode: 'local',
						    forceSelection: false,
						    triggerAction: 'all',
						    emptyText:'默认仓库',
						    selectOnFocus:true,
							name: 'sname',
								
							anchor:'80%'
					}), {
						xtype: 'numberfield',
						fieldLabel: '接收数量',
						name: 'number',
						anchor: '60%'
					}, {
						xtype: 'textfield',
						fieldLabel: '备注',
						name: 'comment',
						anchor: '95%'
					}]
				},
					
				
				buttons: [{
					text: '确定',
					listeners: {
						"click": function(){
							this.synchronizeJson();
							if(this.formDataJson.number && this.formDataJson.number != "") {								
								this.issure = true;
							}	
							this.popupWindow.hide();													
						}.createDelegate(this)
					}
				}, {
					text: '取消',
					listeners: {
						"click": function(){
							this.issure = false;
							this.popupWindow.hide();
						}.createDelegate(this)
					}
				}]
			});
			
			this.popupWindow = new Ext.Window({
		 		
		 		title: '新入库单',
				closable: true,	
				collapsible  : true,
				width: 400,
				height: 268,
				plain: true,
				frame: true,
				layout: 'fit',
				closeAction :'close',
					
				items: this.inputForm,
					
				listeners: {
					'hide': function() {							
						this.synchronizeJson();
						if(this.issure) {
							SALE.Action.insertProductStocks(this.callback, this.formDataJson);
						}						
					}.createDelegate(this)
				}
		 	});			
		}
	},
	
	show : function() {
		if (!this.popupWindow) {			
			this.initlize();
		}
		
		this.popupWindow.show();
	},
	
	setCallback : function(callback) {
		this.callback = callback;
	},
	
	productsSelectedHandler: function(record) {
		this.inputForm.form.findField("pcode").setValue(record.get("pcode"));
		this.inputForm.form.findField("pname").setValue(record.get("pname"));
	},
	
	slectedProductHandler : function(combox, record) {		
		this.inputForm.form.findField("pcode").setValue(record.get("code"));
		this.inputForm.form.findField("pname").setValue(record.get("name"));
	}
});